﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Lesson : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string page = Request.QueryString["page"];
        string limit = Request.QueryString["limit"];
        int start = (int.Parse(page) - 1) * int.Parse(limit);
        int end = start + int.Parse(limit)+1;
        int count = 0;
        //Course course = new Course("高数", 5, 64, "必修", "教三401", "星期一1,2节");
        MyList<Course> list = new MyList<Course>();
        //list.Add(course);
        MySqlConnection con = DbUtils.getCon();
        MySqlCommand cmd = new MySqlCommand("select cname,tname,lesson.* from lesson,teacher,course" +
            " where lesson.tid=teacher.tid and lesson.cid=course.cid limit "+start+","+end, con);
        MySqlDataReader dr = cmd.ExecuteReader();
        string[,] schedules = new string[5, 7];
        for(int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                schedules[i,j] = "星期" + (j + 1) + "第" + (i + 1) + "节";
            }

        }
        while (dr.Read())
        {
            string time = dr["time"].ToString();
            string classTime = "";
            string[] times = time.Split(',');
            foreach (string str in times)
            {
                int date = int.Parse(str);
                int r = date / 7;
                int c = date % 7 - 1;
                classTime += schedules[r, c]+"  ";
            }
            Course course = new Course();
            course.setCname(dr["cname"].ToString());
            course.setTname(dr["tname"].ToString());
            course.setCredit(int.Parse(dr["credit"].ToString()));
            course.setClasshour(int.Parse(dr["classhour"].ToString()));
            course.setType(dr["type"].ToString());
            course.setTime(classTime);
            course.setPlace(dr["place"].ToString());
            list.Add(course);
        }
        dr.Close();
        cmd = new MySqlCommand("select count(1) count from lesson,teacher,course" +
            " where lesson.tid=teacher.tid and lesson.cid=course.cid", con);
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            count = int.Parse(dr["count"].ToString());
        }
        DbUtils.close(con, dr);
        GridResponse grid = new GridResponse();
        grid.setCode(0);
        grid.setCount(count);
        grid.setMsg("message");
        grid.setData(list);
        Response.Clear();
        Response.ContentType = "application/json; charset=utf-8";
        Response.Write(grid.toString());
        Response.End();
    }
}